package com.gz.moreaseManager.sys.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.sys.entity.Role;
import com.gz.moreaseManager.sys.entity.User;
import com.gz.moreaseManager.sys.service.IMenuService;
import com.gz.moreaseManager.sys.service.IRoleMenuService;
import com.gz.moreaseManager.sys.service.IRoleService;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-19
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;


    /**
     * 角色管理界面
     *
     * @return 页面模板
     */
    @GetMapping("listPage")
    public ModelAndView listPage() {
        return new ModelAndView("sys/roleManage/listPage");
    }

    /**
     * 分页查询角色列表
     *
     * @param roleName 角色名称
     * @param pageNum  查询页码
     * @param pageSize 每页数量
     * @return 分页数据
     */
    @GetMapping("findRolePage")
    public PageResult<Role> findRolePage(String roleName, Integer pageNum, Integer pageSize) {
        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Role>();
        wrapper.like("role_name",roleName);
        Page<Role> data = roleService.selectPage(paramsPage,wrapper);
        return new PageResult<>(0, null, data.getTotal(), data.getRecords());
    }

    /**
     * 批量更新角色状态
     *
     * @param status  角色状态
     * @param roleIds 角色编号
     * @return 更新结果
     */
    @PostMapping("setStatus")
    public AjaxResult<Boolean> setStatus(Byte status, Integer... roleIds) {
        if (roleIds == null || status == null)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误");
        boolean success = roleService.updateRoleStatusByIds(status, roleIds);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "更新角色状态失败!");
    }

    /**
     * 角色编辑数据
     *
     * @param roleId 角色编号
     * @return 角色相关数据
     */
    @GetMapping("editInfo")
    public AjaxResult<?> editPage(Integer roleId) {
        Map<String, Object> data = new HashMap<>();
        data.put("roleInfo", roleService.selectById(roleId));
        data.put("roleMenu", menuService.findMenuListByRoleId(roleId));
        return AjaxResult.success(data);
    }

    /**
     * 更新角色信息
     *
     * @param roleInfo 编辑信息
     * @param menuIds  菜单编号
     * @return 更新结果
     */
    @PostMapping("editInfo")
    public AjaxResult<Boolean> editInfo(Role roleInfo, Integer... menuIds) {
        if (roleInfo == null || roleInfo.getRoleId() == null || UtilTool.isBlank(roleInfo.getRoleName())
                || roleInfo.getStatus() == null)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        if (roleService.checkRoleInfoHasExist(roleInfo.getRoleId(), roleInfo.getRoleName()))
            return AjaxResult.build(HttpCode.DATA_REPEAT, "角色名称已存在!");
        boolean success = roleService.updateRoleInfoById(roleInfo, menuIds);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "更新角色信息失败!");
    }

    /**
     * 添加角色信息
     *
     * @param roleInfo 编辑信息
     * @param menuIds  菜单编号
     * @return 添加结果
     */
    @PostMapping("addNewRole")
    public AjaxResult<Boolean> addNewRole(Role roleInfo, Integer... menuIds) {
        if (roleInfo == null || UtilTool.isBlank(roleInfo.getRoleName()) || roleInfo.getStatus() == null)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        if (roleService.checkRoleInfoHasExist(null, roleInfo.getRoleName()))
            return AjaxResult.build(HttpCode.DATA_REPEAT, "角色名称已存在!");
        boolean success = roleService.addNewSysRoleInfo(roleInfo, menuIds);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "添加角色信息失败!");
    }

    /**
     * 获取所有角色信息
     * @return
     */
    @GetMapping("findAllRoleList")
    public AjaxResult<List<Role>> findAllRoleList(){
        List<Role> roleList = roleService.findAllRoleList();
        return AjaxResult.success(roleList);
    }
}
